#########################################################################################
# Title: Table2.R
# Author: Yuki Atsusaka (atsusaka@rice.edu)
# Aim: Code to replicate Table 2 in Atsusaka (2021)
#########################################################################################

# CLEAN THE GLOBAL ENVIRONMENT AND READ PACKAGES
rm(list=ls())
library(xtable)
library(tidyverse)

##############################################################
# COMPUTE ePCPs
##############################################################
dat.comp <- read_csv(here::here("Data_StateLegislative.csv")) %>%
            filter(unusual==0)
            
df_out <- data.frame(Subset = rep(NA,24),
                     Black  = rep(NA,24),
                     Hispanic = rep(NA,24),
                     Asian = rep(NA,24),
                     BlackWin  = rep(NA,24),
                     HispanicWin = rep(NA,24),
                     AsianWin = rep(NA,24))

nm <- c("All Sample",
        ">97% Biracial", 
        ">95% Biracial", 
        ">93% Biracial", 
        ">90% Biracial", 
        ">85% Biracial", 
        ">80% Biracial",
        "0<C<40", 
        "40<C<65", 
        "65<C<100",        
        "State House", 
        "State Senate", 
        "2012 Presidential",
        "2014 Midterm",        
        "Uncontested Races",
        "Contested Races",
        "South",
        "Deep South",
        "Rim South",
        "Non-South",
        "Former Section 5", 
        "Not Former Section 5",
        "Challenged",
        "Not Challenged")

# SEPERATING THE SAMPLE BY GROUP
df1 <- dat.comp %>% filter(group=="Black")    #  ONLY BLACK CANDIDATE
df2 <- dat.comp %>% filter(group=="Hispanic") # ONLY HISPANIC CANDIDATES
df3 <- dat.comp %>% filter(group=="Asian")    # ONLY ASIAN CANDIDATES
df_list <- list(df1,df2,df3) # LIST OF DATASETS
indicator <- c("Black", "Hispanic", "Asian")

Nvec <- NULL

# LOOP OVER RACIAL GROUP (OUTER LOOP)
for(k in 1:3){              
dat.comp.g <- df_list[[k]]
loop_col <- indicator[k]
loop_col2 <- paste0(loop_col,"Win")

dt1  <- dat.comp.g
dt2  <- dat.comp.g %>% filter((C+white_pct)>=97) # >97% Biracial
dt3  <- dat.comp.g %>% filter((C+white_pct)>=95) # >95% Biracial
dt4  <- dat.comp.g %>% filter((C+white_pct)>=93) # >93% Biracial
dt5  <- dat.comp.g %>% filter((C+white_pct)>=90) # >90% Biracial
dt6  <- dat.comp.g %>% filter((C+white_pct)>=85) # >85% Biracial 
dt7  <- dat.comp.g %>% filter((C+white_pct)>=80) # >80% Biracial
dt8  <- dat.comp.g %>% filter(C < 40)            # Below 40%
dt9  <- dat.comp.g %>% filter(C >= 40 & C < 65)  # 40-65%
dt10 <- dat.comp.g %>% filter(C >= 65)           # Over 65%
dt11 <- dat.comp.g %>% filter(sl_chamber==9)     # State House
dt12 <- dat.comp.g %>% filter(sl_chamber==8)     # State Senate
dt13 <- dat.comp.g %>% filter(year==2012)        # 2012 Presidential Election
dt14 <- dat.comp.g %>% filter(year==2014)        # 2014 Midterm Election
dt15 <- dat.comp.g %>% filter(M %in% c(0,100))   # Uncontested Races
dt16 <- dat.comp.g %>% filter(M>0 & M<100)       # Not Uncontested Races
dt17 <- dat.comp.g %>% filter(south==1)          # South
dt18 <- dat.comp.g %>% filter(deepsouth==1)      # Deep South
dt19 <- dat.comp.g %>% filter(rimsouth==1)       # Rim South
dt20 <- dat.comp.g %>% filter(south==0)          # non-South
dt21 <- dat.comp.g %>% filter(section5==1)       # Former Section 5
dt22 <- dat.comp.g %>% filter(section5==0)       # Not Former Section 5
dt23 <- dat.comp.g %>% filter(litigated==1)      # Challenged Districts
dt24 <- dat.comp.g %>% filter(litigated==0)      # Not Challenged Districts


dtlist <- list(dt1, dt2, dt3, dt4, dt5,    # Combine as one list
               dt6, dt7, dt8, dt9, dt10,
               dt11, dt12, dt13, dt14, dt15,
               dt16, dt17, dt18, dt19, dt20,
               dt21, dt22, dt23, dt24)

ePCPvec.run <- NULL
ePCPvec.win <- NULL

# LOOP OVER THE DATA LIST (INNER LOOP)
for(i in 1:length(dtlist)){
dt <- dtlist[[i]] # Choose one dataset

N = dim(dt)[1]
M = dt$M      # M: (Adjusted) Racial margin of victory
C = dt$C      # C: Percentage of Minority CVAP

############################
# LOGICAL MODEL
############################
q.model = sqrt(M*C) - 50      # GEOMETRIC MEAN
true.run = dt$minority_run    # OBSERVED MINORITY CANDIDATE EMERGENCE
true.win = dt$minority_win    # OBSERVED MINORITY ELECTORAL SUCCESS

p.model   = pnorm(q=q.model,  mean=0, sd=1)
ePCP.model.run  = (sum(p.model[true.run==1]) + sum(1 - p.model[true.run==0]))/N
ePCP.model.win  = (sum(p.model[true.win==1]) + sum(1 - p.model[true.win==0]))/N

ePCPvec.run[i] <- ePCP.model.run
ePCPvec.win[i] <- ePCP.model.win

Nvec[i] <- N
} # END OF THE INNER LOOP (i)


out.run <- data.frame(round(ePCPvec.run*100, d=1))
out.win <- data.frame(round(ePCPvec.win*100, d=1))

df_out[,paste0(loop_col)] <- out.run
df_out[,paste0(loop_col2)] <- out.win


if(k==1){
  df_out[,"Subset"] <- paste0(nm)}
else{print("nah")}


} # END OF THE OUTTER LOOP (k)


#####################################
# WRITE OUT A TABLE IN LATEX CODE
#####################################
print(df_out)
print(xtable(df_out, digits=c(0,1,1,1,1,1,1,1)),
      include.rownames=FALSE)


#########################################################################################
# END OF THIS R SOURCE FILE
#########################################################################################